package com.digitalchina.dcn.dcss.web.service.impl;

import com.digitalchina.dcn.dcss.core.generic.GenericDao;
import com.digitalchina.dcn.dcss.core.generic.GenericServiceImpl;
import com.digitalchina.dcn.dcss.web.dao.UrlFilterMapper;
import com.digitalchina.dcn.dcss.web.model.Resource;
import com.digitalchina.dcn.dcss.web.model.UrlFilter;
import com.digitalchina.dcn.dcss.web.service.ResourceService;
import com.digitalchina.dcn.dcss.web.service.RoleService;
import com.digitalchina.dcn.dcss.web.service.ShiroFilterChainManager;
import com.digitalchina.dcn.dcss.web.service.UrlFilterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * Created by Administrator on 2016/7/15 0015.
 */
@Service
public class UrlFilterServiceImpl extends GenericServiceImpl<UrlFilter, Long> implements UrlFilterService {

    @Autowired
    private UrlFilterMapper urlFilterMapper;

    @Autowired
    private RoleService roleService;

    @Autowired
    private ShiroFilterChainManager shiroFilerChainManager;

    @PostConstruct
    public void initFilterChain() {
        shiroFilerChainManager.initFilterChains(findAll());
    }

    @Override
    public GenericDao<UrlFilter, Long> getDao() {
        return urlFilterMapper;
    }

    @Override
    public UrlFilter createUrlFilter(UrlFilter urlFilter) {
        urlFilterMapper.insert(urlFilter);
        initFilterChain();
        return urlFilterMapper.findByName(urlFilter.getName());
    }

    @Override
    public UrlFilter updateUrlFilter(UrlFilter urlFilter) {
        urlFilterMapper.updateByPrimaryKey(urlFilter);
        initFilterChain();
        return urlFilterMapper.findByName(urlFilter.getName());
    }

    @Override
    public void deleteUrlFilter(Long urlFilterId) {
        urlFilterMapper.deleteByPrimaryKey(urlFilterId);
        initFilterChain();
    }

    @Override
    public UrlFilter findOne(Long urlFilterId) {
        return urlFilterMapper.selectByPrimaryKey(urlFilterId);
    }

    @Override
    public List<UrlFilter> findAll() {
        return urlFilterMapper.findAll();
    }
}
